package my.suveng.oauth2.controller;

import my.suveng.model.common.interfaces.response.IMessage;
import my.suveng.model.common.response.element.ElementPage;
import my.suveng.model.common.response.element.PageQuery;
import my.suveng.oauth2.model.dto.*;
import my.suveng.oauth2.service.db.mysql.model.entity.SysUser;
import my.suveng.oauth2.service.db.mysql.model.entity.SysUserRole;
import my.suveng.oauth2.service.transaction.UserTransactionService;
import my.suveng.oauth2.service.user.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户控制器
 *
 * @author suwenguang
 **/
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private UserTransactionService userTransactionService;

    /**
     * 发送验证码
     *
     * @author suwenguang
     */
    @PostMapping(value = "/verifyCode/email")
    public IMessage<Object> verifyCode(@RequestBody VerifyCodeDto dto) {
        // 发送验证码
        return userService.sendEmailVerfiyCode(dto.getEmail());
    }

    /**
     * 注册
     *
     * @author suwenguang
     */
    @PostMapping("/register")
    public IMessage<Object> register(@RequestBody RegisterDto dto) {
        // 业务逻辑
        return userService.register(dto);
    }

    /**
     * 获取oauth的user信息
     *
     * @author suwenguang
     */

    @PostMapping("/info")
    public User getUserInfoByUsername(@RequestBody UserDto dto) {
        return userService.getUserInfoByUsername(dto.getUsername());
    }

    /**
     * 用户列表
     *
     * @author suwenguang
     */
    @PostMapping("/list")
    public IMessage<ElementPage<List<SysUser>>> userList(@RequestBody PageQuery<UserSearch> query) {
        return userService.userList(query);
    }

    /**
     * 增加用户
     *
     * @author suwenguang
     */
    @PostMapping("/add")
    public IMessage<SysUser> add(@RequestBody UserAddDto userAddDto) {
        return userService.addUser(userAddDto);
    }

    /**
     * 编辑用户
     *
     * @author suwenguang
     */
    @PostMapping("/edit")
    public IMessage<Object> edit(@RequestBody UserEditDto userEditDto) {
        return userService.edit(userEditDto);
    }

    /**
     * 删除用户
     *
     * @author suwenguang
     */
    @PostMapping("/delete")
    public IMessage<Object> delete(@RequestBody UserDeleteDto userDeleteDto) {
        return userTransactionService.delete(userDeleteDto);
    }

    /**
     * 用户增加角色
     *
     * @author suwenguang
     */
    @PostMapping("/add/role")
    public IMessage<List<SysUserRole>> addRole(@RequestBody UserAddRoleDto userAddRoleDto) {
        return userService.addRoles(userAddRoleDto);
    }

}
